/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-09-26
 * Time:21:32
 */


import java.util.concurrent.CountDownLatch;

/**
 * 编写代码, 基于 CountDownLatch 模拟跑步比赛的过程
 */
public class MyCountDownLatch {
    public static void main(String[] args) throws InterruptedException {
        //有10个选手参加比赛
        CountDownLatch countDownLatch = new CountDownLatch(10);

        for (int i = 0; i < 10; i++) {
            //创建10个线程来执行任务
            Thread thread = new Thread(()->{
                System.out.println("开始比赛"+Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("抵达终点"+Thread.currentThread().getName());
                //撞线
                countDownLatch.countDown();

            });
            thread.start();
        }
        //await是进行阻塞等待,会等到所有的选手都撞线后,才解除阻塞
        countDownLatch.await();
        System.out.println("比赛结束");
    }
}
